package com.example.rbac.config;
import com.example.rbac.pojo.SysUser;
import com.example.rbac.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
//自定义认证过程
//这个注解是用来实现bean的注入的
@Component
public class SysSecurityService implements UserDetailsService {
    @Autowired
    private SysUserRepository sysUserRepository;
    //自定义登录认证过程，根据用户获取用户信息，如果用户被锁定，则抛出异常
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser=sysUserRepository.findByName(username);
        if(null==sysUser)
            throw new UsernameNotFoundException("用户名不存在");
        else if(!sysUser.getEnabled())
            throw new LockedException("用户被锁定");
        return (UserDetails) sysUser;
    }

}
